草庐IT

Python Django 模板 : Iterate Through List

全部标签

c++ - 模板实例化歧义

我正在玩弄一个templatedimplementation一个FSM并且遇到如下歧义:/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:Ininstantiationof‘voidfsm::FSM::Event(std::unique_ptr)[withEventType=AddEvent;FSMBaseState=EventBaseState]’:/home/permal/code/FSM/Test/test.cpp:83:44:requiredfromhere/home/permal/code/FSM/Test/../FSM

c++ - 类型组的模板类的成员特化?

我有一个矩阵类,我想针对不同的矩阵类型(int、float、double)以不同的方式将矩阵打印到终端。我想实现这个:如果矩阵类型ifint,使用printf("%d",matrix[i][j])打印矩阵如果矩阵类型iffloat或double,使用printf("%.3f",matrix[i][j])打印矩阵否则,抛出错误以下是我所拥有的相关部分:...templateclassMatrix2D{private:std::vector>matrix;public:...voidprint()const;//printthewholematrix}...templatevoidMatr

c++ - 如何模板化外部数组?

我创建了一个代码文件来保存我所有的全局变量,其中之一是一个像这样的数组:全局.cpp#includearrayarr={"value1","value2","value3"};我在另一个代码文件中测试数组值,如下所示:测试数组.cpp#include#includetemplatevoidTestingArrays(constarray&ArrT);voidArrayTester(){externarrayarr;arraylocalarr={"v1","v2"};TestingArrays(localarr);TestingArrays(arr);}templatevoidTesti

c++ - 具有函数和模拟部分模板特化的 SFINAE

我正在尝试创建一个函数X,它在提供成员函数Y时专门化,如果未提供成员函数Y,则函数X使用全局非成员函数Y来实现相同的效果。我目前正在尝试使用以下代码实现此目的template().y()),decltype(std::declval().y())>::value>*=nullptr>voiddo_something(Container&&container){returnstd::forward().y();}template().y()),decltype(std::declval().y())>::value>*=nullptr>voiddo_something(Container

C++ 嵌套模板 : inaccessible static method

我在以下代码中遇到了一个奇怪的(或者可能不是)错误:templateclassRegistrer{public:Registrer(){Registry::register(T::instance);}};templateclassRegisteringClass{private:staticconstRegistrerREGISTRER;public:RegisteringClass(){Q_UNUSED(REGISTRER);/*forcestaticinstantiation*/}staticconstWhatEver*instance(){staticTINSTANCE;ret

c++ - 如何在共享指针中为模板类使用前向声明(在 C++ 中)

我有一个基类Base在Base.h中定义:classBase{/*...*/};还有一个类模板Child源自Base,在Child.h中定义:#include"Base.h"templateclassChild:publicBase{/*...*/};现在我想在Base中创建一些工厂方法类,它应该返回一个std::shared_ptr到Child类(class)。为了避免循环依赖,我尝试使用前向声明。所以Base.h现在看起来像这样:classChild;//newforwarddeclarationclassBase{/*...*///newfactorymethodstaticst

c++ - 使用(模板化的)删除函数重载来防止通常的算术转换

我经常发现我想阻止特定构造函数或函数的缩小或符号转换(通常是Usualarithmeticconversions)。我倾向于写:#includevoidfoo(doublef){std::coutvoidfoo(T&&f)=delete;voidbar(unsignedintf){std::coutvoidbar(T&&f)=delete;这样就可以了...intmain(){autoi=2;autod=2.0;autof=2.0f;foo(i);//preventedfoo(d);//OKfoo(f);//preventedautouil=3ull;autoul=3ul;autou=

c++ - 未定义对 clang 中函数指针的变量模板的引用,但不是 gcc

#includestaticconstexprboolisSSE2=true;templatestaticvoid(*fp)();templatestaticvoidfoo_c(){std::coutstaticvoidfoo_sse2(){std::cout=foo_sse2;elsefp=foo_c;fp();return0;}我有一个使用变量模板的项目,它本身就是一个指向函数的指针。上面的示例代码在GCC6.3中编译和执行良好,但在clang3.9.1中给出警告和错误。$clang++"Source.cpp"-o"foo.exe"-std=c++14-O2Source.cpp:6

c++ - 模板声明顺序在 C++ 中重要吗

以下代码摘自http://www.gotw.ca/publications/mill17.htm#includeusingnamespacestd;template//(a)abasetemplatevoidf(T){cout//(b)asecondbasetemplate,overloads(a)voidf(T*){cout//(c)explicitspecializationof(b)voidf(int*){cout上述情况的输出是“base3”。但是如果我在(b)上面写(c),输出是“base2”。我在cpp.sh测试了上面的代码。谁能告诉我原因吗?

C++ variadic 可变参数模板参数的数量

有一个可变参数模板很简单,我可以专门化它所以它只接受TStringConstant那是一个string_constant一些char小号:templateclassentry;templateclassentry,TValue>{}如果我想创建一个模板类来接受可变数字TStringConstant不同的chars,有办法吗?也许使用模板模板参数?因此以下所有内容都是有效的:entry_list,string_constant>();entry_list,string_constant,string_constant>();entry_list>();如果它会拒绝则奖励entry_lis